﻿/// <reference path="../jquery.ext.js" />
/// <reference path="../jquery.list.js" />

var selectedId = 0;
$(function () {
    $("#btnAdd").click(function () {
        $("#div_device_edit_body").clearArea();
        $("#deviceModal").find(".box-title").text("新增设备");
        //弹出新增窗体
        $("#deviceModal").modal("show");
    });
    $("#deviceModal").find(".btnSave").click(function () {
        $.loading();
        $.sPost("/device/edit", $("#deviceModal").getArea(),
            function (data) {
                if (data.result) {
                    $("#btnLoad").click();
                    $("#deviceModal").modal("hide");
                } else {
                    layer.alert(data.message);
                }
                $.closeLoading();
            });
    });
    $("#btnDelete").click(function () {
        var ids = "";
        $(".checkboxs").each(function () {
            if ($(this).prop("checked") === true) {
                ids += $(this).val() + ",";
            }
        });
        ids = ids.substring(0, ids.length - 1);
        if (ids.length === 0) {
            layer.alert("请选择要删除的记录。");
            return;
        }
        //询问框
        layer.confirm("您确认删除选定的记录吗？", {
            btn: ["确定", "取消"]
        }, function () {
            var sendData = { "ids": ids };
            $.sPost("/device/delete", { ids: ids }, function (box) {
                $("#btnLoad").click();
            });
        });
    });
    $("#checkAll").click(function () {
        var state = $("#checkAll").prop("checked");

        $(".checkboxs").each(function () {
            if (state === true) {
                $(this).prop("checked", true);

            }
            if (state === false) {
                $(this).prop("checked", false);
            }
        });
    });
    //查询按钮
    $("#btnLoad").click(function () {
        layer.load();
        //绑定数据
        $("#condition").queryList({
            pagerId: "#grid_paging_part",
            handleUrl: "/device/getList",
            useUrlParam: true, /*首页URL传入参数需要启用*/
            listId: "#tb_Device",
            templateId: "#div_Device_Row",
            param: {},
            afterLoad: function () { layer.closeAll('loading'); },
            //处理数据
            onLoad: function (data) {
                data = $.map(data, function (item) {
                    item.startDate = $.formatDateString(item.startDate);
                    item.endDate = $.formatDateString(item.endDate);
                    item.stateText = item.state === 0 ? "正常" : "禁用";
                    return item;
                });
            },
            //行处理
            rowFormat: function (itemCtrl, itemData) {
                $(itemCtrl).find(".label").addClass(itemData.state === 0 ? "Normal" : "Disabled");
                $(itemCtrl).find(".edit").on("click", function () {
                    $.sPost("/device/get", { id: itemData.id }, function (box) {
                        $("#div_device_edit_body").setArea(box.data);
                        $("#Title").text("编辑用户");
                        $("#deviceModal").modal("show");
                    });
                });
                $(itemCtrl).find(".delete").on("click", function () {
                    layer.confirm("您确认删除选定的记录吗？", {
                        btn: ["确定", "取消"]
                    }, function () {
                        $.sPost("/device/delete", { id: itemData.id }, function (box) {
                            $(this).remove();
                            //$("#btnLoad").click();
                            layer.closeAll();
                        });
                    });
                });

            }
        });
    }).click();

});